package com.wink_172.library.view;

import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


public class CustomEditText extends EditText {
    private InputMethodManager mInputMethodManager;


    public CustomEditText(Context context) {
        super(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private void initView() {

        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ") || source.toString().contentEquals("\n")) return "";
                else return null;
            }
        };

        mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);//befor
        setFilters(new InputFilter[]{filter});//设置禁止输入空格和换行符


//        //清除焦点 默认不被选中
//        clearFocus();
//        setSelected(false);

    }

    //通过反射改变光标的颜色

    //    private void modifyCursorDrawable(Context context, AttributeSet attrs){
//        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HWEditText);
//        int drawable = a.getResourceId(R.styleable.HWEditText_textCursorDrawable,0);
//        if(drawable != 0) {
//            try {
//
//                Field setCursor = TextView.class.getDeclaredField("mCursorDrawableRes");
//                setCursor.setAccessible(true);
//                setCursor.set(this, drawable);
//
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
//        a.recycle();
//    }
    private static final String TAG = "TAG";

    public void hideSoftInput() {
        if (mInputMethodManager != null) {
            mInputMethodManager.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
        }
    }

    public void showSoftInput() {
        if (mInputMethodManager != null) {
            requestFocus();
            mInputMethodManager.showSoftInput(this, InputMethodManager.SHOW_FORCED);
        }
    }

    public void inputNum() {
        setInputType(InputType.TYPE_CLASS_NUMBER);
    }

    public void inputDecimalNum() {
        setInputType(8194);
//        8194就是android:inputType="numberDecimal"，
    }

    public static String stringFilter(String str) throws PatternSyntaxException {
        // 只允许字母、数字和汉字      
        String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

}
